package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqUserLogin;
import com.lanlian.commonlib.bean.RespUserLogin;
import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.commonlib.util.OnEventID;
import com.lanlian.commonlib.util.OnEventUtils;
import com.lanlian.utillib.WorksSizeCheckUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 8:56
 *     desc   : 登录页面
 *     version: 1.0
 * </pre>
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtAccount;
    private EditText mEtPassword;
    private ImageView mImgPassword;
    private TextView mTvLogin;
    private TextView mTvRegister;
    private ImageView mImgBack;

    private boolean mIsPasswordSee = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        setListeners();
        WorksSizeCheckUtil worksSizeCheckUtil = new WorksSizeCheckUtil(mTvLogin);
        worksSizeCheckUtil.addAllEditText(mEtPassword, mEtAccount);
        worksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    mTvLogin.setBackgroundResource(R.mipmap.login_btn);
                } else {
                    mTvLogin.setBackgroundResource(R.drawable.bg_click_false);
                }
            }
        });
        mEtAccount.setText(mDBManager.getAccountNo());
    }

    @Override
    protected void initView() {
        super.initView();
        mEtAccount = findViewById(R.id.et_account);
        mEtPassword = findViewById(R.id.et_password);
        mImgPassword = findViewById(R.id.img_password);
        mTvLogin = findViewById(R.id.tv_login);
        mTvRegister = findViewById(R.id.tv_register);
        mImgBack = findViewById(R.id.img_back);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mImgPassword.setOnClickListener(this);
        mTvLogin.setOnClickListener(this);
        mTvRegister.setOnClickListener(this);
        mImgBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.img_password:
                if (mIsPasswordSee) {
                    mIsPasswordSee = false;
                    mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    mImgPassword.setImageResource(R.mipmap.login_icon_visible);
                } else {
                    mIsPasswordSee = true;
                    mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    mImgPassword.setImageResource(R.mipmap.login_icon_invisible);
                }
                mEtPassword.setSelection(mEtPassword.length());
                break;
            case R.id.tv_login:
                String account = mEtAccount.getText().toString();
                String password = mEtPassword.getText().toString();
                if (TextUtils.isEmpty(account)) {
                    showToast("请输入账号");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    showToast("请输入密码");
                    return;
                }
                ReqUserLogin reqUserLogin = new ReqUserLogin();
                reqUserLogin.setPwd(password);
                reqUserLogin.setUserName(account);
                showLoadingDialog("登录中");
                VolleyRequestUtils.getInstance().userLogin(this.toString(), reqUserLogin, new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        RespUserLogin respUserLogin = (RespUserLogin) response;
                        loginAction(respUserLogin, new LoginFinishListener() {
                            @Override
                            public void onSuccess() {
                                dismissLoadingDialog();
                                showToast("登录成功");
                                if (getIntent().getIntExtra("tag", 0) == 1) {
                                    EventBusUtils.post(new EventMessage(EventCode.EVENT_LOGIN_TIPS));
                                }
                                finish();
                            }

                            @Override
                            public void onFailure() {
                                dismissLoadingDialog();
                                showToast("登录失败，请稍后再试");
                            }
                        });
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        dismissLoadingDialog();
                        showToast(baseResponse.getMsg());
                    }
                });
                break;
            case R.id.tv_register:
                OnEventUtils.onEvent(this, OnEventID.OPEN_ACCOUNT);
                startActivity(new Intent(this, RegisterMobileActivity.class));
                break;
            case R.id.img_back:
                finish();
                break;
            default:
                break;
        }
    }
}
